//
//  MIDIhairController.m
//  迷笛
//
//  Created by . on 2018/9/13.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDIhairController.h"
#import "MIDIhairCell.h"
#import "MIDImycommandMoel.h"
@interface MIDIhairController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView   * tableView;
@property(nonatomic,copy)NSString * apptoken;
@property(nonatomic,copy)NSString * mycommapi;

@property(nonatomic,strong)NSMutableArray * mycommandArr;
//@property(nonatomic,copy)NSString * apptoken;

@end

@implementation MIDIhairController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];

    [self mycommentsRequest];
    
    self.mycommandArr = [NSMutableArray array];
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(Block:)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"后退"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    self.view.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
    [self HomeUI];
    
}
-(void)mycommentsRequest
{
    

    self.mycommapi = [NSString stringWithFormat:@"%@users/comment?token=%@",MIDIApiRoot,_apptoken];
    [EdHttp get:self.mycommapi params:nil progress:^(id downloadProgress)
    {
        
    }
        success:^(id responseObject)
    {
        NSLog(@"我的评论%@",responseObject);
        
        NSMutableDictionary * dic = responseObject[@"data"];
        
        NSArray *arr4 = dic[@"comment"];
        
        for (NSDictionary *dic4 in arr4)
        {
            NSDictionary * user;
            NSDictionary * dynamic;

            NSString * content;
            NSString * avatar;
            NSString * nickname;
            
            NSString * path;
            NSString * title;

            if (![dic4[@"content"] isKindOfClass:[NSNull class]])
            {
                content = dic4[@"content"];
            }
            if (![dic4[@"user"] isKindOfClass:[NSNull class]])
            {
                user = dic4[@"user"];
            }
            
            if (![user[@"avatar"] isKindOfClass:[NSNull class]])
            {
               avatar  = user[@"avatar"];
            }
            if (![user[@"nickname"] isKindOfClass:[NSNull class]])
            {
                nickname = user[@"nickname"];
            }
            
            if (![dic4[@"dynamic"] isKindOfClass:[NSNull class]])
            {
                dynamic = dic4[@"dynamic"];
            }
            
            if (![dynamic[@"path"] isKindOfClass:[NSNull class]])
            {
                path  = dynamic[@"path"];
            }
            if (![dynamic[@"title"] isKindOfClass:[NSNull class]])
            {
                title  = dynamic[@"title"];
            }
            
            
            MIDImycommandMoel  * mact =[MIDImycommandMoel new];
            mact.nickname = nickname;
            mact.content = content;
            mact.avatar = avatar;
            
            mact.title = title;
            mact.path = path;

            [self.mycommandArr addObject:mact];
        
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
        
    }
        failure:^(NSError *error)
    {
        NSLog(@"失败");
    }];
    
    
    
}




// 返回上一级
-(void)Block:(UIButton * )sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.translucent = NO;//Bar的模糊效果，默认为YES
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#171717"];
    self.title = @"我的评论";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"],NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:WidthScale(17)]}];
    //    去掉导航黑线
    UIImage *img = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.mycommandArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDIhairCell * cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[MIDIhairCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

    }
    //    cell.headiamge.image =[UIImage imageNamed:@"IMG_0195.jpg"];
    MIDImycommandMoel * mm1 = self.mycommandArr[indexPath.row];
    
    [cell.headiamge sd_setBackgroundImageWithURL:mm1.avatar forState:0];

    cell.name.text = mm1.nickname;
    
    cell.content.text = mm1.content;
    
    [cell.iamge sd_setImageWithURL:[NSURL URLWithString:mm1.path] placeholderImage:nil];

    cell.smallText.text = mm1.title;
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
    
}

-(void)HomeUI
{
    [self.view addSubview:self.tableView];
}
-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -35, self.view.frame.size.width, self.view.frame.size.height - 30) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
    }
    return _tableView;
}

@end
